﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyWords
{
    public partial class Recite : Form
    {
        CurrentUser user;
        List<Word> words = new List<Word>();
        Word currentWord;
        public Recite(CurrentUser user)
        {
            this.user = user;
            InitializeComponent();
        }

        private void Recite_Load(object sender, EventArgs e)
        {
            SQLiteDataReader reader = CurrentUser.sql.ReadFullTable("allWords");

            //currentWord = new Word();
            // 加载allWords表中全部的单词到listbox
            while (reader.Read())
            {
                string wordID = reader.GetString(reader.GetOrdinal("wordID"));
                string word = reader.GetString(reader.GetOrdinal("word"));
                string mean = reader.GetString(reader.GetOrdinal("mean"));
                string prop = reader.GetString(reader.GetOrdinal("prop"));
                words.Add(new Word(word,mean,prop,wordID,0));
                listbox_wordList.Items.Add(word);
            }
            reader = CurrentUser.sql.ReadFullTable("myList");
            listbox_wordList.SelectedIndex = 0;


            // 从myList表中查找与现在用户ID相同的标记生词，并将标记信息更新到本地words集合
            while (reader.Read())
            {
                string userID = reader.GetString(reader.GetOrdinal("userID"));
                string wordID = reader.GetString(reader.GetOrdinal("wordID"));
                if (CurrentUser.ID.Equals(userID))
                {
                    for(int i = 0; i < words.Count; i++)
                    {
                        if (words[i].wordID.Equals(wordID))
                        {
                            words[i].isM = 1;
                            
                        }
                    }
                }
            }

            // 初始化显示
            currentWord = words[listbox_wordList.SelectedIndex];
            text_word.Text = currentWord.word;
            text_meaning.Text = currentWord.mean;
            text_prop.Text = currentWord.prop;
            if (currentWord.isM == 1) text_isM.Text = "*";
            else text_isM.Text = "";
        }

        private void listbox_wordList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 重新选择单词时，更新界面显示
            currentWord = words[listbox_wordList.SelectedIndex];
            text_word.Text = currentWord.word;
            text_meaning.Text = currentWord.mean;
            text_prop.Text = currentWord.prop;
            if (currentWord.isM == 1) text_isM.Text = "*";
            else text_isM.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            currentWord.doMark(); // 调用单词的添加/移除标记功能
            if (currentWord.isM == 1) text_isM.Text = "*";
            else text_isM.Text = "";
        }
        //下一个单词
        private void button1_Click(object sender, EventArgs e)
        {
            //防止溢出
            if(listbox_wordList.SelectedIndex+1 < listbox_wordList.Items.Count)
                listbox_wordList.SelectedIndex++;
        }
        // 上一个单词
        private void button2_Click(object sender, EventArgs e)
        {
            //防止溢出
            if (listbox_wordList.SelectedIndex > 0)
                listbox_wordList.SelectedIndex--;
        }
    }
}
